package com.itheima.tlias84.controller;

import com.itheima.tlias84.Po.InsertClazzParam;
import com.itheima.tlias84.Po.SelectClazzPageParam;
import com.itheima.tlias84.Po.UpdateClazzParam;
import com.itheima.tlias84.Service.ClazzService;
import com.itheima.tlias84.Util.ResultUtil;
import com.itheima.tlias84.dto.SelectClazzDTO;
import com.itheima.tlias84.entity.Clazz;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@CrossOrigin
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Resource
    ClazzService clazzService;

    //查询所有班级
    @GetMapping("list")
    public ResultUtil selectAll() {
        List<SelectClazzDTO> selectClazzDTOS = clazzService.selectAll();
        return ResultUtil.success(selectClazzDTOS);

    }

    //根据id查询班级
    @GetMapping("/{id}")
    public ResultUtil selectClazzById(@PathVariable("id") Integer id) {
        SelectClazzDTO selectClazzDTO = clazzService.selectClazzById(id);
        return ResultUtil.success(selectClazzDTO);
    }

    //分页查询班级
    @GetMapping
    public ResultUtil selectClazzByPage(SelectClazzPageParam param) {
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())) {
            return ResultUtil.fail("参数错误");
        }
        ResultUtil resultUtil = clazzService.selectClazzByPage(param);
        return resultUtil;
    }

    //新增班级
    @PostMapping
    public ResultUtil insertClazz(@RequestBody InsertClazzParam param) {
        int i = clazzService.insertClazz(param);
        return i == 0 ? ResultUtil.fail("新增失败") : ResultUtil.success("新增成功");
    }

    //删除班级
    @DeleteMapping("id")
    public ResultUtil deleteById(@RequestParam Integer id) {
        int i = clazzService.deleteById(id);
        return i == 0 ? ResultUtil.fail("删除失败") : ResultUtil.success("删除成功");
    }

    //修改班级
    @PutMapping
    public ResultUtil updateClazz(@RequestBody Clazz clazz) {
        return clazzService.updateClazz(clazz);
    }
}
